package cn.whpu.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
public class DruidConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    //后台监控
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        //后台需要有人登录，账号密码设置
        HashMap<String, String> initParamenters = new HashMap<>();

        //增加配置
        initParamenters.put("loginUsername","admin");
        initParamenters.put("loginPassword","12345");

        //允许谁能访问
        initParamenters.put("allow","localhost");
        //禁止谁能访问
        initParamenters.put("1111","192.168.1.123");

        bean.setInitParameters(initParamenters);//设置初始化参数
        return  bean;
    }



}
